import datetime
import logging

from flask import Flask
from database import db
from config import DevConfig

from course.course_blueprint import course_blue
from user.user_blueprint import user_blue
from index import index_blue
from admin.admin_blueprint import admin_blueprint
from order.order_blueprint import order_blueprint
from file.file_blueprint import file_blueprint

from flask_apscheduler import APScheduler

# 注册全局flask
app = Flask(__name__)
app.config.from_object(DevConfig)

# 初始化定时器
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()

# 注册数据库
db.init_app(app)

# 注册蓝图
app.register_blueprint(course_blue)
app.register_blueprint(user_blue)
app.register_blueprint(index_blue)
app.register_blueprint(admin_blueprint, url_prefix='/admin')
app.register_blueprint(order_blueprint, url_prefix='/api')
app.register_blueprint(file_blueprint, url_prefix='/file')

logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s',
                    filename='/root/miniapp-' + datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S') + '.log')


@app.route('/')
def hello_world():
    return '<h1>Hello World!</h1>', 200


if __name__ == '__main__':
    logging.info('Running miniapp backend at http://localhost:5000/...')
    logging.info('Warning: You are running this project in development mode!')
    app.run(host='0.0.0.0', port=5000)
